using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace Mpi.Common.Collections
{
    public class KeyKeyValueCollection<K1,K2,V>
    {
 
        private Dictionary<K1, Dictionary<K2, V>> innerCollection;

        public V this[K1 key1,K2 key2]
        {
            get
            {
                
                return this.innerCollection[key1][key2];
            }
            set
            {
                this.innerCollection[key1][key2] = value;
            }
        }

        public int Count
        {
            get
            {
                int i = 0;
                foreach (K1 k in innerCollection.Keys)
                {
                    i += innerCollection[k].Count;
                }
                return i;
            }
        }

        public KeyKeyValueCollection()
        {
           this.innerCollection = new Dictionary<K1, Dictionary<K2, V>>();
        }

        public bool Contains(K1 key1, K2 key2)
        {
            if (this.innerCollection.ContainsKey(key1))
            {
                return innerCollection[key1].ContainsKey(key2);
            }
            else
                return false;
        }
        public void Add(K1 key1, K2 key2, V val)
        {
            if (!this.innerCollection.ContainsKey(key1))
            {
                Dictionary<K2, V> coll = new Dictionary<K2, V>();
                innerCollection.Add(key1, coll);
            }
            innerCollection[key1].Add(key2, val);
            return;
        }

        public void Remove(K1 key1, K2 key2)
        {
            innerCollection[key1].Remove(key2);
        }

        public List<K1> FirstLevelKeys()
        {
          
            return new List<K1>(this.innerCollection.Keys);
          
        }

        public List<K2> SecondLevelKeys(K1 k1)
        {
                         
               return new List<K2>(this.innerCollection[k1].Keys);
       
        }
    }
    
}
